import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * value 包含单位测试类
 * 验证 chartTypeList 中 value 字段包含单位的效果
 */
public class ValueWithUnitTest {
    
    public static void main(String[] args) {
        System.out.println("=== value 包含单位测试 ===");
        
        // 测试格式化方法
        testFormatValueWithUnit();
        
        // 测试数值提取方法
        testExtractNumericValue();
    }
    
    /**
     * 测试格式化数值并添加单位
     */
    private static void testFormatValueWithUnit() {
        System.out.println("\n--- 格式化数值并添加单位测试 ---");
        
        // 重量类型测试
        testFormat("672.669", "吨", "672.67吨");
        testFormat("0.686", "吨", "0.69吨");
        testFormat("0.037", "吨", "0.04吨");
        testFormat("0.000119", "吨", "0.00吨");
        testFormat("999.99", "吨", "999.99吨");
        testFormat("1000", "吨", "1.00千吨");
        testFormat("1500.5", "吨", "1.50千吨");
        testFormat("2345.67", "吨", "2.35千吨");
        testFormat("12000", "吨", "12.00千吨");
        
        System.out.println();
        
        // 计数类型测试
        testFormat("3.7", "台", "4台");
        testFormat("10.2", "台", "10台");
        testFormat("5.3", "辆", "5辆");
        testFormat("15.8", "辆", "16辆");
        testFormat("10.4", "个", "10个");
        testFormat("1000.6", "个", "1001个");
        testFormat("50.2", "件", "50件");
    }
    
    /**
     * 测试从包含单位的字符串中提取数值
     */
    private static void testExtractNumericValue() {
        System.out.println("\n--- 数值提取测试 ---");
        
        testExtract("672.67吨", "672.67");
        testExtract("0.69吨", "0.69");
        testExtract("1.50千吨", "1.50");
        testExtract("4台", "4");
        testExtract("16辆", "16");
        testExtract("1001个", "1001");
        testExtract("50件", "50");
        testExtract("", "0");
        testExtract("abc", "0");
        testExtract("123.45", "123.45");
    }
    
    /**
     * 测试单个格式化
     */
    private static void testFormat(String valueStr, String unit, String expected) {
        BigDecimal value = new BigDecimal(valueStr);
        String result = formatValueWithUnit(value, unit);
        String status = result.equals(expected) ? "✓" : "✗";
        System.out.println(status + " " + valueStr + unit + " -> " + result + " (期望: " + expected + ")");
    }
    
    /**
     * 测试单个数值提取
     */
    private static void testExtract(String valueWithUnit, String expected) {
        BigDecimal result = extractNumericValue(valueWithUnit);
        String status = result.toString().equals(expected) ? "✓" : "✗";
        System.out.println(status + " \"" + valueWithUnit + "\" -> " + result + " (期望: " + expected + ")");
    }
    
    /**
     * 格式化数值并添加单位
     */
    private static String formatValueWithUnit(BigDecimal value, String unit) {
        if (value == null) {
            value = BigDecimal.ZERO;
        }
        
        String unitLower = unit.toLowerCase().trim();
        switch (unitLower) {
            case "吨":
            case "t":
                // 重量类型：根据大小决定显示吨还是千吨
                BigDecimal formattedWeight = value.setScale(2, RoundingMode.HALF_UP);
                if (formattedWeight.compareTo(new BigDecimal("1000")) >= 0) {
                    BigDecimal weightInKiloTons = formattedWeight.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);
                    return weightInKiloTons.toString() + "千吨";
                } else {
                    return formattedWeight.toString() + "吨";
                }
            case "台":
            case "辆":
            case "个":
            case "件":
                // 计数类型：保留整数并添加单位
                BigDecimal countValue = value.setScale(0, RoundingMode.HALF_UP);
                return countValue.toString() + unit;
            default:
                // 其他类型：保留2位小数
                BigDecimal defaultValue = value.setScale(2, RoundingMode.HALF_UP);
                return defaultValue.toString();
        }
    }
    
    /**
     * 从包含单位的字符串中提取数值
     */
    private static BigDecimal extractNumericValue(String valueWithUnit) {
        if (valueWithUnit == null || valueWithUnit.trim().isEmpty()) {
            return BigDecimal.ZERO;
        }
        
        // 使用正则表达式提取数字部分（包括小数点）
        String numericPart = valueWithUnit.replaceAll("[^0-9.]", "");
        
        try {
            return new BigDecimal(numericPart);
        } catch (NumberFormatException e) {
            return BigDecimal.ZERO;
        }
    }
}
